import calendar
import datetime
from app import db
from sqlalchemy import func
from models.payment import PaymentPay
def get_month_day():
    today = datetime.date.today()
    month_range = calendar.monthrange(today.year,today.month)[1]
    first_day = datetime.date(today.year, today.month, 1)
    last_day = first_day-datetime.timedelta(1)
    last_month_range = calendar.monthrange(last_day.year,last_day.month)[1]
    month_list = []
    last_month_list = []
    for i in range(month_range):
        month_list.append(first_day+datetime.timedelta(i))
    for i in range(1,last_month_range+1):
        last_month_list.append(last_day-datetime.timedelta(last_month_range-i))

    return month_list,last_month_list

def get_first_last():
    today = datetime.date.today()
    month_range = calendar.monthrange(today.year, today.month)[1]
    first_day = datetime.date(today.year, today.month, 1)
    last_day = datetime.date(today.year, today.month, month_range)
    last_last_day = first_day - datetime.timedelta(1)
    last_first_day = datetime.date(last_last_day.year, last_last_day.month, 1)
    return first_day,last_day,last_first_day,last_last_day

def charge_date_between(start=None,end=None):
    obj = db.session.query(func.sum(PaymentPay.user_pay_money).label('total'))
    if start and end:
        obj = obj.filter(PaymentPay.order_date.between(start,end)).first()
    else:
        obj = obj.filter(PaymentPay.order_date==datetime.date.today()).first()
    if obj[0]:
        return obj.total
    return 0

def get_rang_date(t):
    today = datetime.date.today()
    if t == 1:
        start_date = today-datetime.timedelta(today.weekday())
        end_date = today + datetime.timedelta(start_date.weekday()+6)
    if t == 2:
        month_range = calendar.monthrange(today.year, today.month)[1]
        start_date = datetime.date(today.year, today.month, 1)
        end_date = datetime.date(today.year, today.month, month_range)
    if t == 3:
        start_date = datetime.date(today.year,1,1)
        end_date = datetime.date(today.year,12,31)
    return start_date,end_date




if __name__ == '__main__':
    print(get_rang_date())